package com.lw.controller;

import cn.dev33.satoken.annotation.SaIgnore;
import cn.dev33.satoken.stp.StpUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.lw.common.other.LogOperation;
import com.lw.common.utils.ResultVo;
import com.lw.common.vo.MenuVo;
import com.lw.entity.TbUser;
import com.lw.service.TbUserService;
import com.mybatisflex.core.paginate.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;
import com.lw.entity.TbMenu;
import com.lw.service.TbMenuService;

import java.util.List;

/**
 *  控制层。
 *
 * @author lw
 * @since 2025年05月04日 09:38:56
 */
@RestController
@Tag(name = "菜单接口", description = "菜单相关操作接口")
@RequestMapping("/tbMenu")
@SaIgnore
public class TbMenuController {

    @Resource
    private TbMenuService tbMenuService;

    @Resource
    private TbUserService tbUserService;
    /**
     * 添加。
     *
     * @param tbMenu 
     * @return {@code true} 添加成功，{@code false} 添加失败
     */
    @PostMapping("save")
    @Operation(summary = "保存")
    @LogOperation("菜单-保存")
    public ResultVo<Boolean>  save(@RequestBody TbMenu tbMenu) {
        //判断parentId
        if(tbMenu.getParentId() == 0){
            tbMenu.setParentId(null);
        }
        return ResultVo.success(tbMenuService.save(tbMenu));
    }

    /**
     * 根据主键删除。
     *
     * @param id 主键
     * @return {@code true} 删除成功，{@code false} 删除失败
     */
    @DeleteMapping("remove/{id}")
    @Operation(summary = "根据主键")
    @LogOperation("菜单-删除")
    public ResultVo<Boolean>  remove(@PathVariable @Parameter(description = "主键") Integer id) {
        return ResultVo.success(tbMenuService.removeById(id));
    }

    /**
     * 根据主键更新。
     * @return {@code true} 更新成功，{@code false} 更新失败
     */
    @PutMapping("update")
    @Operation(summary = "根据主键更新")
    @LogOperation("菜单-更新")
    public ResultVo<Boolean> update(@RequestBody @Parameter(description = "主键") TbMenu tbMenu) {
        return ResultVo.success(tbMenuService.updateById(tbMenu));
    }

    /**
     * 查询所有。
     *
     * @return 所有数据
     */
    @GetMapping("list")
    @Operation(summary = "查询所有")
    public ResultVo<List<TbMenu>> list() {
        return ResultVo.success(tbMenuService.list());
    }

    /**
     * 根据主键获取详细信息。
     *
     * @param id 主键
     * @return 详情
     */
    @GetMapping("getInfo/{id}")
    @Operation(summary = "根据主键获取")
    public ResultVo<TbMenu> getInfo(@PathVariable @Parameter(description = "主键") Integer id) {
        return ResultVo.success(tbMenuService.getById(id));
    }

    /**
     * 分页查询。
     * @return 分页对象
     */
    @GetMapping("page")
    @Operation(summary = "分页查询")
    public ResultVo<Page<TbMenu>> page(@Parameter(description = "分页信息")  TbMenu menu,
                                       @RequestParam(defaultValue = "1") int pageNumber,
                                       @RequestParam(defaultValue = "10") int pageSize) {
        Page page = new Page(pageNumber, pageSize);
        return ResultVo.success(tbMenuService.page(page));
    }


    @Operation(summary = "获取当前用户tree结构菜单--Vue模式（前端）")
    @GetMapping("tree")
    public ResultVo<JSONArray>getMenuTree() {
        Integer userId = StpUtil.getLoginIdAsInt();
        TbUser user = tbUserService.getById(userId);
        Integer roleId = user.getRoleId();
        if (user.getLoginName().equalsIgnoreCase("admin")) {
            roleId = null;
        }
        List<TbMenu> menuTree = tbMenuService.getMenuTree(roleId);
        List<MenuVo> menuTree1 = tbMenuService.convertToMenuVoList(menuTree);
        String jsonString = JSONArray.toJSONString(menuTree1);
        JSONArray objects = JSONObject.parseArray(jsonString);
        return ResultVo.success(objects);
    }

    @Operation(summary = "获取当前用户tree结构菜单--纯净模式（数据库）")
    @GetMapping("treePurity")
    public ResultVo<List<TbMenu>> treePurity() {
        List<TbMenu> menuTree = tbMenuService.getMenuTree(null);
        return ResultVo.success(menuTree);
    }
}
